﻿#region Using region
using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.Threading;
using System.Collections.Specialized;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;


using JigLibX.Physics;
using JigLibX.Collision;
using JigLibX.Geometry;
using JigLibX.Math;
using JigLibX.Utils;
using sqengine.PhysicObjects;
using JigLibX.Vehicles;
using System.Diagnostics;



using sqengine.Engine;
using sqengine.Components;

using sqengine.Engine.Common;



using SqEngine.Components;
using SqEngine.Audio;
using SqEngine;


using PK3Model;
#endregion

namespace sqengine
{
    partial class sqengine : Microsoft.Xna.Framework.Game
    {
        private StreamWriter st_trace;

        private void TraceLogEnable(bool enable)
        {
            if (enable)
            {
                st_trace = new StreamWriter(GetTraceLogFilename(), true);
                Functions.TraceLogWrite(st_trace, true, SqEngineInfo._full_version, "TraceLog Initialized", false);
                
                Components.ComponentAdded += new EventHandler<GameComponentCollectionEventArgs>(Components_ComponentAdded);
                Components.ComponentRemoved += new EventHandler<GameComponentCollectionEventArgs>(Components_ComponentRemoved);
                adcontent.OnAdContentLoad += new AdvancedContent.AdContentLoadHandler(adcontent_OnAdContentLoad);
            }            
        }

        void adcontent_OnAdContentLoad(object sender, AdvancedContent.AdContentArgs args)
        {
            Functions.TraceLogWrite(st_trace, false, "", string.Format("AdContent: {0} {1} loaded", args.ObjType(), args.Filename()), false);
        }

        private string GetTraceLogFilename()
        {
            return string.Format("{0}_{1}{2}{3}.log", SqEngineInfo._name, DateTime.Today.Year.ToString(), DateTime.Today.Month.ToString(), DateTime.Today.Day.ToString());

        }
        void Components_ComponentRemoved(object sender, GameComponentCollectionEventArgs e)
        {
            Functions.TraceLogWrite(st_trace, false, "", string.Format("Game Component {0} removed", e.GameComponent.ToString()), false);
        }

        void Components_ComponentAdded(object sender, GameComponentCollectionEventArgs e)
        {
            Functions.TraceLogWrite(st_trace, false, "", string.Format("Game Component {0} added", e.GameComponent.ToString()),false);

        }

        protected override void OnExiting(object sender, EventArgs args)
        {
            if (isTraceLogEnabled)
            {
                
                Functions.TraceLogWrite(st_trace, false, "", "Exiting...", true);
                //
            }

            base.OnExiting(sender, args);
        }

        protected override bool ShowMissingRequirementMessage(Exception exception)
        {
            if (isTraceLogEnabled)
            {
                Functions.TraceLogWrite(st_trace, false, "", string.Format("ShowMissingRequirementMessage(): Message: {0} ", exception.Message), false);
                Functions.TraceLogWrite(st_trace, false, "", string.Format("ShowMissingRequirementMessage(): StackTrace: {0} ", exception.StackTrace), false);
                Functions.TraceLogWrite(st_trace, false, "", string.Format("ShowMissingRequirementMessage(): InnerException: {0} ", exception.InnerException), false);
 
            }
            return base.ShowMissingRequirementMessage(exception);
        }

      
    }
}
